package se.vgregion.icc.riv.itintegration.monitoring;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import se.riv.itintegration.monitoring.pingforconfiguration.v1.rivtabp21.PingForConfigurationResponderInterface;
import se.riv.itintegration.monitoring.pingforconfigurationresponder.v1.ObjectFactory;
import se.riv.itintegration.monitoring.pingforconfigurationresponder.v1.PingForConfigurationResponseType;
import se.riv.itintegration.monitoring.pingforconfigurationresponder.v1.PingForConfigurationType;
import se.vgregion.icc.ResponseHelper;

/***
 * PingForConfigurationConsumer that calls pingForConfigurationComponent
 * for the address that you send in to the constructor.
 * 
 * @author andan40 & ricra2
 * @see {@link se.vgregion.icc.riv.itintegration.monitoring.AbstractConsumer}
 */
public class PingForConfigurationConsumer extends AbstractConsumer<PingForConfigurationResponderInterface> {

    private static final Logger LOG = LoggerFactory.getLogger(PingForConfigurationConsumer.class);
    private static final String NOT_RESPONDING = "NOT RESPONDING";
    private String serviceAddress;

    /***
     * The constructor that calls the AbstractConsumer constructor with the address you want to ping.
     * 
     * @param address the address that you want to call
     */
    public PingForConfigurationConsumer(String address) {
        super(address);
        this.serviceAddress = address;
    }

    /***
     * Get the response (PingForConfigurationResponseType) from the service you want to call.
     * 
     * @param parameters containing already pinged url's.
     * @return the response from the service
     */
    public PingForConfigurationResponseType getResponse(PingForConfigurationType parameters) {

        PingForConfigurationResponseType pingForConfiguration = null;
        try {
            pingForConfiguration = getService().pingForConfiguration(this.serviceAddress, parameters);
        } catch (Exception e) {
            LOG.error("Failed to call the service address = " + this.serviceAddress + ".");

            ObjectFactory objectFactory = new ObjectFactory();
            pingForConfiguration = objectFactory.createPingForConfigurationResponseType();

            ResponseHelper.addConfiguration(pingForConfiguration, "URL", serviceAddress);
            ResponseHelper.addConfiguration(pingForConfiguration, "Response", NOT_RESPONDING);
        }
        return pingForConfiguration;
    }
}
